package com.project.frba.bifront.client.view.dashboard;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.layout.TableLayout;
import com.project.frba.bifront.client.model.Dashboard;
import com.project.frba.bifront.client.rpc.ChartServices;
import com.project.frba.bifront.client.rpc.ChartServicesAsync;
import com.project.frba.bifront.client.view.MessageWindow;

public class QuadDashboardPanel extends Composite{
	
	private Panel mainPanel = new Panel();
	private Map<Integer, String> imagesUrl = new HashMap<Integer, String>();
	private Dashboard dashboard = null;
	private Image firstImage = new Image();	
	private Image secondImage = new Image();		
	private Image thirdImage = new Image();	
	private Image fourthImage = new Image();		
	
	public QuadDashboardPanel(Dashboard dashboard) {
		this.dashboard = dashboard;
		mainPanel.setLayout(new TableLayout(2));
		mainPanel.setWidth(1280);
		mainPanel.setHeight(590);
		loadDashboard();
		
		mainPanel.add(firstImage);
		mainPanel.add(secondImage);
		mainPanel.add(thirdImage);
		mainPanel.add(fourthImage);
		
		
		initWidget(mainPanel);
	}
	
	public void initAll(){
		if (imagesUrl.get(1) != null) {
			firstImage.setUrl("./uploadServer/" + imagesUrl.get(1));
		} else {
			firstImage.setUrl("./icons/empty4.png");
		}
		
		if (imagesUrl.get(2) != null) {
			secondImage.setUrl("./uploadServer/" + imagesUrl.get(2));
		} else {
			secondImage.setUrl("./icons/empty4.png");
		}
		
		if (imagesUrl.get(3) != null) {
			thirdImage.setUrl("./uploadServer/" + imagesUrl.get(3));
		} else {
			thirdImage.setUrl("./icons/empty4.png");
		}
		
		if (imagesUrl.get(4) != null) {
			fourthImage.setUrl("./uploadServer/" + imagesUrl.get(4));
		} else {
			fourthImage.setUrl("./icons/empty4.png");
		}
		
	}
	
	public void loadDashboard(){
		AsyncCallback<Map<Integer, String>> callback = new AsyncCallback<Map<Integer, String>>(){
			public void onSuccess(Map<Integer, String> urls) {
				imagesUrl = urls;
				initAll();
			}
			
			public void onFailure(Throwable caught) {
				new MessageWindow("Error al cargar las imagenes de los tableros de comandos.\n Por favor intente de nuevo.");
			}
		};
		ChartServicesAsync chartServices = (ChartServicesAsync)GWT.create(ChartServices.class);
		chartServices.doChart(dashboard, callback);
	}

}
